package com.ruge.system.controller;

import com.ruge.common.util.RestResponse;
import com.ruge.common.util.ShiroUtil;
import com.ruge.system.pojo.entity.SysUser;
import com.ruge.system.pojo.request.UserRequest;
import com.ruge.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2020-03-04  15:53
 * 描述 ：
 * @author dell
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户controller", tags = {"用户操作接口"})
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation(value = "获取用户列表信息", notes = "获取用户列表信息")
    public RestResponse list(UserRequest request) {
        return RestResponse.success().setData(sysUserService.findAll(request));
    }
    @GetMapping("currentUser")
    @ApiOperation(value = "获取用户列表信息", notes = "获取用户列表信息")
    public RestResponse currentUser() {
        SysUser user = sysUserService.findByLoginName(ShiroUtil.getActiveUser().getUser().getLoginName());
        return RestResponse.success().setData(user);
    }

    @PostMapping
    @ApiOperation(value = "保存用户详细信息")
    public RestResponse save(@RequestBody SysUser entity) {
        return RestResponse.success().setData(sysUserService.save(entity));
    }

    @ApiOperation(value = "更新用户详细信息")
    @PutMapping
    public RestResponse update(@RequestBody SysUser entity) {
        return RestResponse.success().setData(sysUserService.save(entity));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    public RestResponse delete(@PathVariable Long id) {
        sysUserService.deleteById(id);
        return RestResponse.success();
    }

    @ApiOperation(value = "批量操作", notes = "根据url的id来指定批量操作对象")
    @PostMapping("/batch")
    public RestResponse batch(@RequestBody Map<String, List<SysUser>> map) {
        return RestResponse.success().setData(sysUserService.batch(map));
    }

    @PostMapping("/authRole")
    @ApiOperation(value = "给用户进行菜单授权详细信息")
    public RestResponse authRole(@RequestBody List<UserRequest> entity) {
        sysUserService.authRole(entity);
        return RestResponse.success().setMsg("授权成功");
    }

    @PostMapping("/resetPwd")
    @ApiOperation(value = "用户密码重置")
    public RestResponse resetPwd(@RequestBody UserRequest entity) {
        sysUserService.resetPwd(entity);
        return RestResponse.success().setMsg("重置成功");
    }

    @PostMapping("/updatePwd")
    @ApiOperation(value = "用户密码修改")
    public RestResponse updatePwd(@RequestBody UserRequest request) {
         String msg = sysUserService.updatePwd(request);
        if (!"密码修改成功".equals(msg)) {
            return RestResponse.failure(msg);
        }
        return RestResponse.success().setMsg(msg);
    }
}
